package com.zhixun.mobile.modules.jiujiudevice.device.util;

import java.util.Calendar;
import java.util.Date;

public class DateAddUtils {
	
	/**
	 * 增加时间
	 * @param hour
	 * @return
	 */
	public static Date addHour(int hour){
		Date date =new Date();
		Calendar ca=Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.HOUR_OF_DAY, hour);
		ca.set(Calendar.SECOND, 59);
		ca.set(Calendar.MILLISECOND,999);
		return ca.getTime();
	}

	/**
	 * 增加时间
	 * @param hour
	 * @return
	 */
	public static Date addHour(Date date,int hour){
		Calendar ca=Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.HOUR_OF_DAY, hour);
		ca.set(Calendar.SECOND, 59);
		ca.set(Calendar.MILLISECOND,999);
		return ca.getTime();
	}
	
	
	
	/**
	 * 增加时间和分钟
	 * @param hour
	 * @return
	 */
	public static Date addHourAndMinute(int hour,int minute){
		Date date =new Date();
		Calendar ca=Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.HOUR_OF_DAY,hour);
		ca.add(Calendar.MINUTE, minute);
		ca.set(Calendar.SECOND, 59);
		ca.set(Calendar.MILLISECOND,999);
		return ca.getTime();
	}
	
	/**
	 * 增加时间和分钟
	 * @param hour
	 * @return
	 */
	public static Date addHourAndMinute(Date date,int hour,int minute){
	
		Calendar ca=Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.HOUR_OF_DAY,hour);
		ca.add(Calendar.MINUTE, minute);
		ca.set(Calendar.SECOND, 59);
		ca.set(Calendar.MILLISECOND,999);
		return ca.getTime();
	}
	
	/**
	 * 计算时间差
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static String calculateTimeDifference(Date d1, Date d2){
		 long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
	     long days = diff / (1000 * 60 * 60 * 24);
	     long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
	     long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
	     return days+"天"+hours+"小时"+minutes+"分";
	}
	

	/**
	 * 计算时间差之后的秒数   用于redis保存
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long calculateTimeDifferenceM(Date d1, Date d2){
		 long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
	     long days = diff / (1000 * 60 * 60 * 24);
	     long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
	     long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
	     long m = 1;
	     if(days > 0){
	    	m += days*24*60*60;
	     }
	     if(hours > 0){
	    	m+= hours*60*60;
	     }
	     if(minutes > 0){
	    	m+= m*60;
	     }    
	     return m;
	}
	
	
	
	public static  void main(String [] args){
		Date date1 = new Date();
		Date date2 = addHour(2);
		System.out.println(calculateTimeDifference(date2,date1));
	}
	
	
}
